﻿using System.Collections.Generic;

namespace Framework.Resource
{
    /// <summary>
    /// 资源缓存
    /// </summary>
    public class AssetCache
    {
        /// <summary>
        /// 资源包集合
        /// </summary>
        private Dictionary<string, AssetPackage> m_Packages = null;

        /// <summary>
        /// 所有资源包集合
        /// </summary>
        public List<AssetPackage> AllPackage
        {
            get
            {
                List<AssetPackage> list = new List<AssetPackage>();
                foreach (var item in m_Packages)
                {
                    list.Add(item.Value);
                }
                return list;
            }
        }
        /// <summary>
        /// 所有资源包名称集合
        /// </summary>
        public List<string> AllPackageName
        {
            get
            {
                List<string> list = new List<string>();
                foreach (var item in m_Packages)
                {
                    list.Add(item.Key);
                }
                return list;
            }
        }
        /// <summary>
        /// 资源包数量
        /// </summary>
        public int PackageCount
        {
            get
            {
                return m_Packages.Count;
            }
        }

        /// <summary>
        /// 构造资源缓存
        /// </summary>
        public AssetCache()
        {
            m_Packages = new Dictionary<string, AssetPackage>();
        }
        /// <summary>
        /// 获得资源包
        /// </summary>
        /// <param name="packageName">资源包名称</param>
        public AssetPackage GetAssetPackage(string packageName)
        {
            if (string.IsNullOrEmpty(packageName)) return null;
            if (m_Packages.ContainsKey(packageName)) return m_Packages[packageName];
            return null;
        }
        /// <summary>
        /// 添加资源包
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="package"></param>
        public void AddAssetPackage(string packageName, AssetPackage package)
        {
            if (string.IsNullOrEmpty(packageName) || package == null || m_Packages.ContainsKey(packageName)) return;
            m_Packages.Add(packageName, package);
        }
        /// <summary>
        /// 释放所有的缓存
        /// </summary>
        public void ReleaseAllCache()
        {
            List<AssetPackage> packages = AllPackage;
            for (int i = 0; i < packages.Count; i++)
            {
                packages[i].ReleasePackage();
            }
            m_Packages.Clear();
        }
        /// <summary>
        /// 释放所有未被占用的缓存
        /// </summary>
        public void ReleaseUnusedCache()
        {
            bool isRelease = false;
            List<AssetPackage> packages = AllPackage;
            for (int i = packages.Count - 1; i >= 0; i--)
            {
                AssetPackage package = packages[i];
                if(package.IsFree == true)
                {
                    m_Packages.Remove(package.PackageName);
                    package.ReleasePackage();
                    isRelease = true;
                }
            }
            if (isRelease == true)
                ReleaseUnusedCache();
        }
        /// <summary>
        /// 是否包含资源包
        /// </summary>
        /// <param name="packageName">资源包名称</param>
        public bool HasPackage(string packageName)
        {
            return m_Packages.ContainsKey(packageName);
        }
        /// <summary>
        /// 释放资源包
        /// </summary>
        /// <param name="packageName">资源包名称</param>
        /// <param name="isForce">是否强制释放</param>
        public void ReleasePackage(string packageName, bool isForce = false)
        {
            if (string.IsNullOrEmpty(packageName)) return;
            AssetPackage package = null;
            if(m_Packages.TryGetValue(packageName, out package))
            {
                if (package == null) return;
                if(isForce == true)
                {
                    package.ReleasePackage();
                    m_Packages.Remove(packageName);
                }
                else
                {
                    if (package.IsFree)
                    {
                        package.ReleasePackage();
                        m_Packages.Remove(packageName);
                    }
                }
            }
        }
    }
}
